package com.tb.ClassSchedule;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.PopupWindow;

/**
 * Created by guich on 2016/8/12.
 */
public class AddButtonMenu extends ImageButton {

    private static MainActivity main = MainActivity.main;

    private static PopupWindow addButtonMenu;

    //单例模式
    static {
        new AddButtonMenu(main);
    }
    private static ImageButton addButton;

    //获取该例
    public static ImageButton getInstance(){
        return addButton;
    }
    //私有构造函数
    private AddButtonMenu(Context context) {
        super(context);

        loadSrc();
        setOnClickListener();

    }


    private void loadSrc(){
//        addButton = (ImageButton) main.findViewById(R.id.setting);
        View view = LayoutInflater.from(main).inflate(R.layout.menu_layout,null);
        addButtonMenu = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.MATCH_PARENT,true);
        addButtonMenu.setBackgroundDrawable(new BitmapDrawable());
        addButtonMenu.setOutsideTouchable(true);
        addButtonMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                addButton.setImageBitmap(BitmapFactory.decodeResource(main.getResources(),R.drawable.add));
            }
        });

        View classManager = view.findViewById(R.id.class_manager_button);
        View setWeek = view.findViewById(R.id.set_week_button);
        View tableBg = view.findViewById(R.id.table_bg_button);
        View changeView = view.findViewById(R.id.chang_view_button);
        View popupBg = view.findViewById(R.id.popup_bg);
        classManager.setOnClickListener(main);
        setWeek.setOnClickListener(main);
        tableBg.setOnClickListener(main);
        changeView.setOnClickListener(main);
        popupBg.setOnClickListener(main);

    }

    private void setOnClickListener(){
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showMenu();
            }
        });
    }

    public static void showMenu() {
        addButton.setImageBitmap(BitmapFactory.decodeResource(main.getResources(),R.drawable.close));
        addButtonMenu.showAsDropDown(addButton);
    }

    public static void onClick(View view){
        Intent intent = new Intent();
        switch (view.getId()){

            case R.id.class_manager_button:
//                Toast.makeText(main,"添加课程功能研发中，敬请期待。。。",Toast.LENGTH_SHORT).show();
                intent.setClass(main,AddClassActivity.class);
                main.startActivity(intent);
                addButtonMenu.dismiss();
                break;
            case R.id.set_week_button:
                TimeManager.setCurrentWeekDialog();
                addButtonMenu.dismiss();
                break;
            case R.id.table_bg_button:
//                Toast.makeText(main,"设置背景功能研发中，敬请期待。。。",Toast.LENGTH_SHORT).show();
                intent.setClass(main,SetThemeActivity.class);
                main.startActivity(intent);
                addButtonMenu.dismiss();
                break;
            case R.id.chang_view_button:
                MainActivity.mainViewMode = (MainActivity.mainViewMode + 1) % 2;
                SharedPreferences.Editor editor = MainActivity.sharedPreferences.edit();
                editor.putInt("mainViewMode",MainActivity.mainViewMode);
                editor.commit();
                editor.clear();

                main.draw();
                addButtonMenu.dismiss();
                break;
            case R.id.popup_bg:
                addButtonMenu.dismiss();
                break;

        }
    }

    public static void onKeyUpEvent(int keyCode) {
        switch (keyCode){
            case KeyEvent.KEYCODE_MENU:
                if(addButtonMenu.isShowing()){
                    Log.d("tb","addButtonMenu.isShowing");
                    addButtonMenu.dismiss();
                }else{
                    showMenu();
                }

                break;
        }
    }

}
